package com.liu.my_blog_system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.my_blog_system.entity.TbArticle;
import com.liu.my_blog_system.entity.TbUser;
import com.liu.my_blog_system.mapper.TbArticleMapper;
import com.liu.my_blog_system.service.TbArticleService;
import com.liu.my_blog_system.service.TbUserService;
import com.liu.my_blog_system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * tb_article (TbArticle)表服务实现类
 *
 * @author liuchengjie
 * @since 2021-04-07 14:14:55
 */
@Service()
@Transactional
public class TbArticleServiceImpl extends ServiceImpl<TbArticleMapper, TbArticle> implements TbArticleService {

    @Autowired
    private TbUserService userService;
    /**
     * 新增文章接口
     * @param article
     * @param request
     * @param response
     * @return
     */
    @Override
    public Result addArticle(TbArticle article, HttpServletRequest request, HttpServletResponse response) {
        //检查用户，拿到用户信息
        Map map = userService.checkUserStatus(request, response);
        TbUser userInfo = (TbUser) map.get("userInfo");
        //补充数据
        article.setUserId(userInfo.getId());
        //保存到数据库
        int row = baseMapper.insert(article);
        if (row > 0) {
            return Result.ok().message("保存成功");
        }
        //todo:保存到搜索的数据库
        return Result.error().message("保存失败");
    }

    /**
     * 查询文章
     * @param current
     * @param size
     * @return
     */
    @Override
    public Result getArticleList(Long current, Long size) {
        Page<TbArticle> articlePage = new Page<>(current, size);
        Page<TbArticle> page = baseMapper.selectPage(articlePage, null);
        List<TbArticle> records = page.getRecords();
        long total = page.getTotal();
        return Result.ok().data("total",total).data("records",records);
    }
}



















